package com.ouyang.servlet.User;

import com.ouyang.poji.User;
import com.ouyang.service.User.UserService;
import com.ouyang.service.User.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet ( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
        String UserName = req.getParameter("UserName");
        String UserPassword = req.getParameter("UserPassword");
        UserService userService = new UserServiceImpl();
        User user = userService.login(UserName, UserPassword);
        if (user!=null) {
            System.out.println(user);
            req.getSession().setAttribute("usersession",user);
            resp.sendRedirect(req.getContextPath()+"/admin/index.jsp");
        }else {

            req.setAttribute("error","用户名或密码不正确！");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doPost ( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
        doGet(req, resp);
    }
}
